let app = new Vue({
    el: '#app',
    data: {
        admin: JSON.parse(sessionStorage.getItem("admin")),     
        tabIndex:0,
        paperQuestionIndex:0,
        paperQuestionIds:'',  // 存放试题ID
        scores:0              // 计算试题总分

    },
    created() {

        layui.use('form', function () {

            $.ajax({
                url: 'http://localhost:8080/subject/selectLevelNameByDistinct',
                type: 'get',
                success: function (vo) {
                    if (vo.code == 200) {
                        $.each(vo.data, function (i, s) {
                            $("#levelName").append("<option value='" + s.levelName + "'>" + s.levelName + "</option>");
                        });
                    }
                    layui.form.render();
                }
            });

        });



    },
    methods: {
        save: function () {
            // 非空判断
            if ($("#levelName").val() == '' || $("#subjectName").val() == '' || $("#paperType").val() == '' || $("#suggestTime").val() == '' || $("#examPaperName").val() == '') {
                layer.msg("必填字段不能为空", { offset: 't', icon: 5 });
                return;
            }
            if($("#paperType").val() == 2 && ($("#limitStartTime").val()=='' || $("#limitEndTime").val()=='')){
                layer.msg("必填字段不能为空", { offset: 't', icon: 5 });
                return;
            }


            
            // 获取试题编号、题目总分和题目总数
            let arrId=$("#titleContent .hiddenPaperQuestionId");
            let arrScore=$("#titleContent .hiddenScore");
            let questionCount=0;
            $.each(arrId,function(){
                app.paperQuestionIds=app.paperQuestionIds+$(this).val()+"&";
                questionCount=questionCount+1;
            })
            $.each(arrScore,function(){
                app.scores=app.scores+parseInt($(this).val());
            })
            //去掉最后一个"&"逗号
            if (this.paperQuestionIds.length > 0) {
                this.paperQuestionIds = this.paperQuestionIds.substr(0, this.paperQuestionIds.length - 1);
            }
  
            let formData={
                'examPaperName':$("#examPaperName").val(),
                'paperType':$("#paperType").val(),
                'levelName':$("#levelName").val(),
                'subjectName':$("#subjectName").val(),
                'paperScore':this.scores,
                'questionCount':questionCount,
                'questions':this.paperQuestionIds,
                'suggestTime':$("#suggestTime").val()*60,
                'limitStartTime':$("#limitStartTime").val(),
                'limitEndTime':$("#limitEndTime").val(),
                'createUser':this.admin.userId
            }
            
            $.ajax({
                url: 'http://localhost:8080/examPaper/add',
                type: 'post',
                data: formData,
                success: function (vo) {
                    if (vo.code == 200) {
                        layer.msg(vo.data, { offset: 't', icon: 1 });
                        // 延迟1秒执行代码
                        setTimeout(function () {
                            location.reload();
                        }, 1000);
                        
                    }else{
                        layer.msg(vo.message, { offset: 't', icon: 2 });
                    }
                }
            });
        },
        addTab: function () {
            if(this.tabIndex==3){
                layer.msg("最多添加三个标题", { offset: 't', icon: 5 });
                return;
            }
            this.tabIndex=this.tabIndex+1;
            
            if(this.tabIndex==1){
                $("#titleContent").append(
                    "<div class='layui-form-item' id='did"+this.tabIndex+"'>"+
                        "<label class='layui-form-label'><span style='color: red;'>*</span>标题"+this.tabIndex+"：</label>"+
                        "<div class='layui-inline' style='width: 700px;'>"+
                            "<input type='text' id='title' value='单选题' readonly required lay-verify='required' autocomplete='off' class='layui-input'>"+
                        "</div>"+
                        "<div class='layui-inline' style='font-size:10px;color: #1890ff;cursor: pointer;'>"+
                            "<span onclick='addQuestion("+this.tabIndex+")'>添加题目</span>&nbsp;&nbsp;&nbsp;"+
                        "</div>"+
                    "</div>"
                );
            }else if(this.tabIndex==2){
                $("#titleContent").append(
                    "<div class='layui-form-item' id='did"+this.tabIndex+"'>"+
                        "<label class='layui-form-label'><span style='color: red;'>*</span>标题"+this.tabIndex+"：</label>"+
                        "<div class='layui-inline' style='width: 700px;'>"+
                            "<input type='text' id='title' value='多选题' readonly required lay-verify='required' autocomplete='off' class='layui-input'>"+
                        "</div>"+
                        "<div class='layui-inline' style='font-size:10px;color: #1890ff;cursor: pointer;'>"+
                            "<span onclick='addQuestion("+this.tabIndex+")'>添加题目</span>&nbsp;&nbsp;&nbsp;"+
                        "</div>"+
                    "</div>"
                );
            }else if(this.tabIndex==3){
                $("#titleContent").append(
                    "<div class='layui-form-item' id='did"+this.tabIndex+"'>"+
                        "<label class='layui-form-label'><span style='color: red;'>*</span>标题"+this.tabIndex+"：</label>"+
                        "<div class='layui-inline' style='width: 700px;'>"+
                            "<input type='text' id='title' value='判断题' readonly required lay-verify='required' autocomplete='off' class='layui-input'>"+
                        "</div>"+
                        "<div class='layui-inline' style='font-size:10px;color: #1890ff;cursor: pointer;'>"+
                            "<span onclick='addQuestion("+this.tabIndex+")'>添加题目</span>&nbsp;&nbsp;&nbsp;"+
                        "</div>"+
                    "</div>"
                );
            }
            
        },
        delTab:function(){
            $("#titleContent .layui-form-item:last").remove();
            this.tabIndex=this.tabIndex-1;
        }
        // delQuestion:function(){
        //     $("#titleContent .layui-input-block:last").remove();
        //     this.paperQuestionIndex=this.paperQuestionIndex-1;
        //     if(this.paperQuestionIndex==0){
        //         $("#delQuestionBtn").hide();
        //     }
        // }
    }
});